import { defineStore } from "pinia";
import { store } from "@/store";
import { storage } from "@/utils/storage";
import { zhCN, enUS, jaJP } from "naive-ui";

const useLanguageStore = defineStore("app-language", {
  state: () => ({
    language: storage.get("language") || "zhCN", // 默认语言
    LanguageList: [
      {
        label: "中文",
        key: "zhCN",
        locale: zhCN,
      },
      {
        label: "English",
        key: "enUS",
        locale: enUS,
      },
      {
        label: "日本語",
        key: "jaJP",
        locale: jaJP,
      },
    ],
  }),
  getters: {
    getLanguage() {
      let lang = this.LanguageList.find((item) => item.key === this.language);
      return lang.label;
    },
    getLocaleLanguage() {
      let lang = this.LanguageList.find((item) => item.key === this.language);
      return lang.locale;
    },
  },
  actions: {
    setLanguage(language: string) {
      this.language = language;
      storage.set("language", this.language);
    },
  },
});

export function useLanguage() {
  return useLanguageStore(store);
}
